package com.app.ta.shi.xuantian.controller;

import com.app.ta.shi.xuantian.jopo.AccessTokenResponse;
import com.app.ta.shi.xuantian.too.BaiSuUtils;
import com.dtflys.forest.Forest;
import com.jayway.jsonpath.JsonPath;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.app.ta.shi.xuantian.too.BaiSuUtils.of;
@RestController
public class ApitaShiController {


    @Scheduled(cron = "0 * * ? * *")
    public void yaoYiRen() {

        //获取token
        AccessTokenResponse response = Forest.post("https://app.dtuip.com/oauth/token?grant_type=password&username=15024604789&password=wk123456")
                .addHeader("authorization", "Basic NWNjOTI4ZTA0NGVhNGUxYjg0ZTM4ZWZjZGQ2ZDlkMDU6N2UxOWE3YWRkZDFmNGMwN2E0ZTNjZDU1OGE1N2EzMDQ=")
                .addHeader("content-type", "text/plain")
                .addHeader("Host", "app.dtuip.com")
                .addHeader("cache-control", " no-cache")
                .execute(AccessTokenResponse.class);

        String accessToken = response.getAccessToken();

        //分页获取用户设备传感器(含传感器实时数据)
        String result = Forest.post("https://app.dtuip.com/api/device/getDeviceSensorDatas")
                .addHeader("Authorization", "Bearer " + accessToken + "")
                .addHeader("content-type", "text/plain")
                .addHeader("Host", "app.dtuip.com")
                .addHeader("cache-control", "no-cache")
                .addHeader("tlinkAppId", "5cc928e044ea4e1b84e38efcdd6d9d05")
                .addBody(" {\n" +
                        "    \"userId\": \"112201\",\n" +
                        "    \"currPage\":1,\n" +
                        "    \"pageSize\":20\n" +
                        "}")
                .executeAsString();

        List<Object> read = (List<Object>) JsonPath.read(result, "$.dataList[*].sensorsList[*].value");

        if (read.size() == 0) {
            System.out.println("石塔数据获取失败!!!");
        }

        //向百数添加数据
        // 创建 SimpleDateFormat 对象，指定日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 获取当前时间
        Date currentDate = new Date();
        // 将当前时间格式化为指定格式的字符串
        String formattedDate = sdf.format(currentDate);

        BaiSuUtils utils = of("59fa0ccaaafcbce94b67717c", "5afed942945415856a6aecbc");
        utils.batchCreate(Arrays.asList(
                Map.of(
                        "_widget_1745898344383", "设备1"
                        , "_widget_1745898344416", "设备1"
                        , "_widget_1745898344433", "设备1"
                        , "_widget_1745898344632", read.get(0)
                        , "_widget_1745898344482", formattedDate
                ), Map.of(
                        "_widget_1745898344383", "设备2"
                        , "_widget_1745898344416", "设备2"
                        , "_widget_1745898344433", "设备2"
                        , "_widget_1745898344632", read.get(1)
                        , "_widget_1745898344482", formattedDate
                ), Map.of(
                        "_widget_1745898344383", "设备3"
                        , "_widget_1745898344416", "设备3"
                        , "_widget_1745898344433", "设备3"
                        , "_widget_1745898344632", read.get(2)
                        , "_widget_1745898344482", formattedDate
                ), Map.of(
                        "_widget_1745898344383", "设备4"
                        , "_widget_1745898344416", "设备4"
                        , "_widget_1745898344433", "设备4"
                        , "_widget_1745898344632", read.get(3)
                        , "_widget_1745898344482", formattedDate
                )
        ));

    }
}